package cn.kgc.controller;

import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;
import cn.kgc.service.ProviderService;
import cn.kgc.util.PageBean;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author 蒿雨
 * @create 2021-11-01 17:57
 */
@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Resource(name = "psi")
    ProviderService providerService;


    //修改方法
    @RequestMapping("/modifyP")
    public String modifyP(@ModelAttribute Provider provider, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        provider.setModifyBy(user.getId());
        provider.setModifyDate(new Date());
        if (providerService.providerModify(provider)) {
            System.out.println("修改成功！");
            return "redirect:/provider/providerlist";
        }
        return "providermodify";

    }

    //修改赋值
    @RequestMapping("//modify/{id}")
    public String modify(@PathVariable int id, HttpSession session) {
        session.setAttribute("provider", providerService.getP(id));
        return "providermodify";
    }


    @RequestMapping("/delete1")
    @ResponseBody
    public String userDel(int proid) {
        System.out.println(proid);
        Map<String, Object> map = new HashMap<>();
        if (providerService.isFull(proid)) {
            map.put("delResult", "notexist");
            return JSON.toJSONString(map);
        }
        if (providerService.delete(proid) > 0) {
            map.put("delResult","true");
            return JSON.toJSONString(map);
        }
        map.put("delResult", "false");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/view/{id}")
    public String view(@PathVariable int id, HttpSession session) {
        session.setAttribute("provider", providerService.getP(id));
        return "providerview";
    }


    @RequestMapping("/providerlist")
    public String getProviderList(HttpSession session, HttpServletRequest request, String pageIndex, String queryProCode, String queryProName) {
        Map<String, Object> map = new HashMap<>();
        int index = 1;
        int pageSize = 5;
        if (pageIndex != null) {
            index = Integer.parseInt(pageIndex);
        }
        map.put("queryProCode", queryProCode);
        map.put("queryProName", queryProName);
        map.put("startIndex", index);
        map.put("pageSize", pageSize);
        PageBean<Provider> pageBean = providerService.getProviderList(map);
        session.setAttribute("providerList", pageBean);
        request.setAttribute("queryProCode", queryProCode);
        request.setAttribute("queryProName", queryProName);

        return "providerlist";
    }

    //跳转到添加页面
    @RequestMapping("/provideraddgo")
    public String addgo() {
        return "provideradd";
    }

    @RequestMapping("/provideraddsave")
    public String add(HttpServletRequest request, @RequestParam(name = "attachs1", required = false) MultipartFile attachs1, @RequestParam("attachs") MultipartFile attachs, Provider provider, HttpSession session) {
        String uploadFileError = "";
        String uploadOcError = "";
        String newFileName = "";
        String newFileName1 = "";

        String uploadPath = "";
        String uploadPath1 = "";

        if (attachs != null) {
            uploadPath = session.getServletContext().getRealPath("statics" + File.separator + "uploadFiles");
            String fileName = attachs.getOriginalFilename();
            if (attachs.getSize() > 5000000) {
                uploadFileError = "上传的文件超过限制大小";
                request.setAttribute("uploadFileError", uploadFileError);
                return "provideradd";
            }
            //前后缀
            String prefix = fileName.substring(0, fileName.indexOf("."));
            String suffix = fileName.substring(fileName.indexOf(".") + 1);
            //判断文件类型
            List<String> list = new ArrayList<>();
            list.add("jpg");
            list.add("jpeg");
            list.add("png");
            if (!list.contains(suffix)) {
                uploadFileError = "上传文件不符合格式!";
                request.setAttribute("uploadFileError", uploadFileError);
                return "useradd";
            }
            //定义上传的文件名
            newFileName = prefix + (int) (Math.random() * (100000 - 10000) + 10000) + "" + System.currentTimeMillis() + "." + suffix;
            //创建文件上传容器(上传的绝对路径,文件名)
            File file = new File(uploadPath, newFileName);
            if (!file.exists()) {
                file.mkdirs();//创建文件夹
            }
            try {
                attachs.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("uploadFileError", "系统错误!请重试!");
                return "provideradd";
            }
        }
        if (attachs1 != null) {
            uploadPath1 = session.getServletContext().getRealPath("statics" + File.separator + "uploadFiles");
            String fileName = attachs1.getOriginalFilename();
            if (attachs1.getSize() > 5000000) {
                uploadOcError = "上传的文件超过限制大小";
                request.setAttribute("uploadOcError", uploadOcError);
                return "provideradd";
            }
            //前后缀
            String prefix = fileName.substring(0, fileName.indexOf("."));
            String suffix = fileName.substring(fileName.indexOf(".") + 1);
            //判断文件类型
            List<String> list = new ArrayList<>();
            list.add("jpg");
            list.add("jpeg");
            list.add("png");
            if (!list.contains(suffix)) {
                uploadOcError = "上传文件不符合格式!";
                request.setAttribute("uploadOcError", uploadOcError);
                return "useradd";
            }
            //定义上传的文件名
            newFileName1 = prefix + (int) (Math.random() * (100000 - 10000) + 10000) + "" + System.currentTimeMillis() + "." + suffix;
            //创建文件上传容器(上传的绝对路径,文件名)
            File file = new File(uploadPath1, newFileName1);
            if (!file.exists()) {
                file.mkdirs();//创建文件夹
            }
            try {
                attachs1.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("uploadOcError", "系统错误!请重试!");
                return "provideradd";
            }
        }


        User user = (User) session.getAttribute("loginUser");
        provider.setCreatedBy(user.getId());
        provider.setCreationDate(new Date());
        provider.setCompanyLicPicPath(uploadPath + newFileName);
        provider.setOrgCodePicPath(uploadPath1 + newFileName1);
        if (providerService.addProvider(provider)) {
            System.out.println("添加成功！");
            return "/providerlist";
        }
        System.out.println("添加失败");
        return "provideradd";
    }

}
